"use strict";

const Controller = require("egg").Controller;

class FileController extends Controller {
  async index() {
    const { ctx, app } = this;

    const { current, size, filename = "", filetype = "" } = ctx.request.body;

    let sql = " ";

    if (filename) {
      sql += `where filename = "${filename}" `;
    } else if (filetype) {
      sql += `where filetype = "${filetype}" `;
    }

    let limit = ` limit ${(current - 1) * size} , ${size}`;

    const data = await app.mysql.query(`select * from file ` + sql + limit);

    const total = (
      await app.mysql.query(`select count(*) as count from file` + sql)
    )[0].count;

    ctx.body = {
      code: 1,
      data,
      total,
    };
  }
}

module.exports = FileController;
